package com.ymatou.iapi.exp.testcase;

import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.exp.parameter.GetUserCoinBean;
import com.ymatou.iapi.exp.service.GetUserCoinCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ExpCallService;
import com.ymttest.database.model.UserCoin;
import com.ymttest.database.sqlwapper.ExpWapper;
import com.ymttest.utils.YMTDateUtil;
/**
*查询获取金币记录
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetUserCoin{
	private static GetUserCoinBean getusercoinBean;
	private static GetUserCoinCall getusercoinCall;
	private static ExpWapper ExpWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询获取金币记录");
	}

	@Before
	public void caseUp() {
		getusercoinBean=new GetUserCoinBean();
		getusercoinCall=new GetUserCoinCall();
		ExpWapper=new ExpWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getusercoinCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserCoin_001() {
		Logger.start(true,"只有今年金币查询获取金币成功");
		try {
			int uid=1001;
			int coin=new Random().nextInt(10)+1;
			ExpCallService.resetCoinAccount(uid);
//			ExpCallService.ChangeUserCoin(uid, "200101", YMTDateUtil.getSysTime("yyyyMMdd"), 15);
			UserCoin uc=new UserCoin();
			uc.setCoin(coin);
			uc.setUserId(uid);
			uc.setYear(Integer.valueOf(YMTDateUtil.getSysTime("yyyy")));
			ExpWapper.insertUserCoin(uc);
			getusercoinBean.setUserId(uid);
			getusercoinCall.setData(getusercoinBean);
			getusercoinCall.callService();
			JsonObject result = getusercoinCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getusercoinCall.getString("BCode"), "验证BCode");
			Logger.verifyEquals(coin, result.get("totalCoin").getAsInt(), "验证totalCoin");
			Logger.verifyEquals(coin, result.get("curCoin").getAsInt(), "验证curCoin");
			Logger.verifyEquals(0, result.get("prevCoin").getAsInt(), "验证prevCoin");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserCoin_002() {
		Logger.start(true,"只有去年金币查询获取金币成功");
		try {
			int uid=1002;
			int coin=new Random().nextInt(10)+1;
			ExpCallService.resetCoinAccount(uid);
//			ExpCallService.ChangeUserCoin(uid, "200101", YMTDateUtil.getSysTime("yyyyMMdd"), 15);
			UserCoin uc=new UserCoin();
			uc.setCoin(coin);
			uc.setUserId(uid);
			uc.setYear(Integer.valueOf(YMTDateUtil.getSysTime("yyyy"))-1);
			ExpWapper.insertUserCoin(uc);
			getusercoinBean.setUserId(uid);
			getusercoinCall.setData(getusercoinBean);
			getusercoinCall.callService();
			JsonObject result = getusercoinCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getusercoinCall.getString("BCode"), "验证BCode");
			Logger.verifyEquals(coin, result.get("totalCoin").getAsInt(), "验证totalCoin");
			Logger.verifyEquals(0, result.get("curCoin").getAsInt(), "验证curCoin");
			Logger.verifyEquals(coin, result.get("prevCoin").getAsInt(), "验证prevCoin");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserCoin_003() {
		Logger.start(true,"今年去年金币查询获取金币成功");
		try {
			int uid=1003;
			int coin=new Random().nextInt(10)+1;
			int bcoin=new Random().nextInt(10)+1;
			ExpCallService.resetCoinAccount(uid);
//			ExpCallService.ChangeUserCoin(uid, "200101", YMTDateUtil.getSysTime("yyyyMMdd"), 15);
			Logger.comment("插入今年金币:"+coin);
			UserCoin uc=new UserCoin();
			uc.setCoin(coin);
			uc.setUserId(uid);
			uc.setYear(Integer.valueOf(YMTDateUtil.getSysTime("yyyy")));
			ExpWapper.insertUserCoin(uc);
			Logger.comment("插入去年金币:"+bcoin);
			uc.setCoin(bcoin);
			uc.setYear(Integer.valueOf(YMTDateUtil.getSysTime("yyyy"))-1);
			ExpWapper.insertUserCoin(uc);
			getusercoinBean.setUserId(uid);
			getusercoinCall.setData(getusercoinBean);
			getusercoinCall.callService();
			JsonObject result = getusercoinCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getusercoinCall.getString("BCode"), "验证BCode");
			Logger.verifyEquals(coin+bcoin, result.get("totalCoin").getAsInt(), "验证totalCoin");
			Logger.verifyEquals(coin, result.get("curCoin").getAsInt(), "验证curCoin");
			Logger.verifyEquals(bcoin, result.get("prevCoin").getAsInt(), "验证prevCoin");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserCoin_004() {
		Logger.start(true,"只有前年数据获取金币成功");
		try {
			int uid=1004;
			int coin=new Random().nextInt(10)+1;
			ExpCallService.resetCoinAccount(uid);
//			ExpCallService.ChangeUserCoin(uid, "200101", YMTDateUtil.getSysTime("yyyyMMdd"), 15);
			UserCoin uc=new UserCoin();
			uc.setCoin(coin);
			uc.setUserId(uid);
			uc.setYear(Integer.valueOf(YMTDateUtil.getSysTime("yyyy"))-2);
			ExpWapper.insertUserCoin(uc);
			getusercoinBean.setUserId(uid);
			getusercoinCall.setData(getusercoinBean);
			getusercoinCall.callService();
			JsonObject result = getusercoinCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getusercoinCall.getString("BCode"), "验证BCode");
			Logger.verifyEquals(0, result.get("totalCoin").getAsInt(), "验证totalCoin");
			Logger.verifyEquals(0, result.get("curCoin").getAsInt(), "验证curCoin");
			Logger.verifyEquals(0, result.get("prevCoin").getAsInt(), "验证prevCoin");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserCoin_005() {
		Logger.start(true,"没有数据获取金币成功");
		try {
			int uid=1005;
			int coin=new Random().nextInt(10)+1;
			ExpCallService.resetCoinAccount(uid);
//			ExpCallService.ChangeUserCoin(uid, "200101", YMTDateUtil.getSysTime("yyyyMMdd"), 15);
			getusercoinBean.setUserId(uid);
			getusercoinCall.setData(getusercoinBean);
			getusercoinCall.callService();
			JsonObject result = getusercoinCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getusercoinCall.getString("BCode"), "验证BCode");
			Logger.verifyEquals(0, result.get("totalCoin").getAsInt(), "验证totalCoin");
			Logger.verifyEquals(0, result.get("curCoin").getAsInt(), "验证curCoin");
			Logger.verifyEquals(0, result.get("prevCoin").getAsInt(), "验证prevCoin");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserCoin_006() {
		Logger.start(true,"消费金币后优先扣除去年金币,查询记录正确");
		try {
			int uid=1006;
			int coin=20;
			int bcoin=10;
			ExpCallService.resetCoinAccount(uid);
//			ExpCallService.ChangeUserCoin(uid, "200101", YMTDateUtil.getSysTime("yyyyMMdd"), 15);
			Logger.comment("插入今年金币:"+coin);
			UserCoin uc=new UserCoin();
			uc.setCoin(coin);
			uc.setUserId(uid);
			uc.setYear(Integer.valueOf(YMTDateUtil.getSysTime("yyyy")));
			ExpWapper.insertUserCoin(uc);
			Logger.comment("插入去年金币:"+bcoin);
			uc.setCoin(bcoin);
			uc.setYear(Integer.valueOf(YMTDateUtil.getSysTime("yyyy"))-1);
			ExpWapper.insertUserCoin(uc);
			getusercoinBean.setUserId(uid);
			getusercoinCall.setData(getusercoinBean);
			getusercoinCall.callService();
			JsonObject result = getusercoinCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getusercoinCall.getString("BCode"), "验证BCode");
			Logger.verifyEquals(coin+bcoin, result.get("totalCoin").getAsInt(), "验证totalCoin");
			Logger.verifyEquals(coin, result.get("curCoin").getAsInt(), "验证curCoin");
			Logger.verifyEquals(bcoin, result.get("prevCoin").getAsInt(), "验证prevCoin");
			int ccoin=9;
			ExpCallService.ChangeUserCoin(uid, "200202", YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"), ccoin);
			getusercoinCall.callService();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getusercoinCall.getString("BCode"), "验证BCode");
			result = getusercoinCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals(coin+bcoin-ccoin, result.get("totalCoin").getAsInt(), "验证totalCoin");
			Logger.verifyEquals(coin, result.get("curCoin").getAsInt(), "验证curCoin");
			Logger.verifyEquals(bcoin-ccoin, result.get("prevCoin").getAsInt(), "验证prevCoin");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserCoin_007() {
		Logger.start(true,"消费金币后优先扣除去年金币,不够用时扣除今年金币,查询记录正确");
		try {
			int uid=1007;
			int coin=20;
			int bcoin=10;
			ExpCallService.resetCoinAccount(uid);
//			ExpCallService.ChangeUserCoin(uid, "200101", YMTDateUtil.getSysTime("yyyyMMdd"), 15);
			Logger.comment("插入今年金币:"+coin);
			UserCoin uc=new UserCoin();
			uc.setCoin(coin);
			uc.setUserId(uid);
			uc.setYear(Integer.valueOf(YMTDateUtil.getSysTime("yyyy")));
			ExpWapper.insertUserCoin(uc);
			Logger.comment("插入去年金币:"+bcoin);
			uc.setCoin(bcoin);
			uc.setYear(Integer.valueOf(YMTDateUtil.getSysTime("yyyy"))-1);
			ExpWapper.insertUserCoin(uc);
			getusercoinBean.setUserId(uid);
			getusercoinCall.setData(getusercoinBean);
			getusercoinCall.callService();
			JsonObject result = getusercoinCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getusercoinCall.getString("BCode"), "验证BCode");
			Logger.verifyEquals(coin+bcoin, result.get("totalCoin").getAsInt(), "验证totalCoin");
			Logger.verifyEquals(coin, result.get("curCoin").getAsInt(), "验证curCoin");
			Logger.verifyEquals(bcoin, result.get("prevCoin").getAsInt(), "验证prevCoin");
			int ccoin=12;
			ExpCallService.ChangeUserCoin(uid, "200202", YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"), ccoin);
			getusercoinCall.callService();
			result = getusercoinCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("0", getusercoinCall.getString("BCode"), "验证BCode");
			Logger.verifyEquals(coin+bcoin-ccoin, result.get("totalCoin").getAsInt(), "验证totalCoin");
			Logger.verifyEquals(coin+bcoin-ccoin, result.get("curCoin").getAsInt(), "验证curCoin");
			Logger.verifyEquals(0, result.get("prevCoin").getAsInt(), "验证prevCoin");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}